package adapter;

import java.util.ArrayList;

import com.example.ass4.DanhbaActivity;
import com.example.ass4.R;
import com.example.ass4.ReadContactActivity;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import data_model.*;

public class MyArrayAdapter2 extends ArrayAdapter<Contact> {
	Activity context = null;
	ArrayList<Contact> myArray = null;
	int layoutId;
	public MyArrayAdapter2(Activity context, int layoutId, ArrayList<Contact> arr) {
		super(context, layoutId, arr);
		this.context = context;
		this.layoutId = layoutId;
		this.myArray = arr;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(layoutId, null);
		final int pos = position;
		
		CheckBox chkItem = (CheckBox) convertView.findViewById(R.id.chkitem);
		if (pos % 2 == 0) {
			convertView.setBackgroundColor(Color.parseColor("#00ced1"));
		} else {
			convertView.setBackgroundColor(Color.parseColor("#3f7fffd4"));
		}
		if (myArray.size() > 0 && position >= 0) {
			final TextView txtItem2 = (TextView) convertView.findViewById(R.id.txtName);
			final TextView txtItem3 = (TextView) convertView.findViewById(R.id.txtNumber);
			final TextView txtItem4 = (TextView) convertView.findViewById(R.id.txtMail);
			
			final Contact cont=myArray.get(position);
			
			txtItem2.setText(cont.getName());
			txtItem3.setText(cont.getNumber());
			txtItem4.setText(cont.getEmail());
			if(myArray.get(pos).isCheck()){
				chkItem.setChecked(true);
			}
			else{
				chkItem.setChecked(false);
			}
			
			chkItem.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
				@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
					if(isChecked){
						myArray.get(pos).setCheck(true);
						((ReadContactActivity)context).startMenu();
					}
					else{
						myArray.get(pos).setCheck(false);
						((ReadContactActivity)context).stopMenu();
					}
				}
		});
			
	}
		return convertView;
	}
}
